home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / alphabuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  6.4 KB  |  264 lines

  1. /* $Id: alphabuf.c,v 1.3 1996/10/02 02:51:07 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.0
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: alphabuf.c,v $
  26.  * Revision 1.3  1996/10/02 02:51:07  brianp
  27.  * in gl_clear_alpha_buffers() check for GL_FRONT_AND_BACK draw mode
  28.  *
  29.  * Revision 1.2  1996/09/15 14:15:54  brianp
  30.  * now use GLframebuffer and GLvisual
  31.  *
  32.  * Revision 1.1  1996/09/13 01:38:16  brianp
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37.  
  38.  
  39. /*
  40.  * Software alpha planes.  Many frame buffers don't have alpha bits so
  41.  * we simulate them in software.
  42.  */
  43.  
  44.  
  45.  
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include "alphabuf.h"
  49. #include "context.h"
  50. #include "macros.h"
  51. #include "types.h"
  52.  
  53.  
  54.  
  55.  
  56. #define ALPHA_ADDR(X,Y)  (ctx->Buffer->Alpha + (Y) * ctx->Buffer->Width + (X))
  57.  
  58.  
  59.  
  60. /*
  61.  * Allocate a new front and back alpha buffer.
  62.  */
  63. void gl_alloc_alpha_buffers( GLcontext* ctx )
  64. {
  65.    GLint bytes = ctx->Buffer->Width * ctx->Buffer->Height * sizeof(GLubyte);
  66.  
  67.    if (ctx->Visual->FrontAlphaEnabled) {
  68.       if (ctx->Buffer->FrontAlpha) {
  69.          free( ctx->Buffer->FrontAlpha );
  70.       }
  71.       ctx->Buffer->FrontAlpha = (GLubyte *) malloc( bytes );
  72.       if (!ctx->Buffer->FrontAlpha) {
  73.          /* out of memory */
  74.          gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate front alpha buffer" );
  75.       }
  76.    }
  77.    if (ctx->Visual->BackAlphaEnabled) {
  78.       if (ctx->Buffer->BackAlpha) {
  79.          free( ctx->Buffer->BackAlpha );
  80.       }
  81.       ctx->Buffer->BackAlpha = (GLubyte *) malloc( bytes );
  82.       if (!ctx->Buffer->BackAlpha) {
  83.          /* out of memory */
  84.          gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate back alpha buffer" );
  85.       }
  86.    }
  87.    if (ctx->Color.DrawBuffer==GL_FRONT) {
  88.       ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha;
  89.    }
  90.    if (ctx->Color.DrawBuffer==GL_BACK) {
  91.       ctx->Buffer->Alpha = ctx->Buffer->BackAlpha;
  92.    }
  93. }
  94.  
  95.  
  96.  
  97. /*
  98.  * Clear the front and/or back alpha planes.
  99.  */
  100. void gl_clear_alpha_buffers( GLcontext* ctx )
  101. {
  102.    GLint buffer;
  103.  
  104.    /* Loop over front and back buffers */
  105.    for (buffer=0;buffer<2;buffer++) {
  106.  
  107.       /* Get pointer to front or back buffer */
  108.       GLubyte *abuffer = NULL;
  109.       if (buffer==0
  110.           && (   ctx->Color.DrawBuffer==GL_FRONT
  111.               || ctx->Color.DrawBuffer==GL_FRONT_AND_BACK)
  112.           && ctx->Visual->FrontAlphaEnabled && ctx->Buffer->FrontAlpha) {
  113.          abuffer = ctx->Buffer->FrontAlpha;
  114.       }
  115.       else if (buffer==1
  116.                && (   ctx->Color.DrawBuffer==GL_BACK
  117.                    || ctx->Color.DrawBuffer==GL_FRONT_AND_BACK)
  118.                && ctx->Visual->BackAlphaEnabled && ctx->Buffer->BackAlpha) {
  119.          abuffer = ctx->Buffer->BackAlpha;
  120.       }
  121.  
  122.       /* Clear the alpha buffer */
  123.       if (abuffer) {
  124.          GLubyte aclear = (GLint) (ctx->Color.ClearColor[3]
  125.                                    * ctx->Visual->AlphaScale);
  126.          if (ctx->Scissor.Enabled) {
  127.             /* clear scissor region */
  128.             GLint i, j;
  129.             for (j=0;j<ctx->Scissor.Height;j++) {
  130.                GLubyte *aptr = ALPHA_ADDR(ctx->Buffer->Xmin,
  131.                                           ctx->Buffer->Ymin+j);
  132.                for (i=0;i<ctx->Scissor.Width;i++) {
  133.                   *aptr++ = aclear;
  134.                }
  135.             }
  136.          }
  137.          else {
  138.             /* clear whole buffer */
  139.             MEMSET( abuffer, aclear, ctx->Buffer->Width*ctx->Buffer->Height );
  140.          }
  141.       }
  142.    }
  143. }
  144.  
  145.  
  146.  
  147. void gl_write_alpha_span( GLcontext* ctx, GLuint n, GLint x, GLint y,
  148.                           GLubyte alpha[], GLubyte mask[] )
  149. {
  150.    GLubyte *aptr = ALPHA_ADDR( x, y );
  151.    GLuint i;
  152.  
  153.    if (mask) {
  154.       for (i=0;i<n;i++) {
  155.          if (mask[i]) {
  156.             *aptr = alpha[i];
  157.          }
  158.          aptr++;
  159.       }
  160.    }
  161.    else {
  162.       for (i=0;i<n;i++) {
  163.          *aptr++ = alpha[i];
  164.       }
  165.    }
  166. }
  167.  
  168.  
  169. void gl_write_mono_alpha_span( GLcontext* ctx, GLuint n, GLint x, GLint y,
  170.                                GLubyte alpha, GLubyte mask[] )
  171. {
  172.    GLubyte *aptr = ALPHA_ADDR( x, y );
  173.    GLuint i;
  174.  
  175.    if (mask) {
  176.       for (i=0;i<n;i++) {
  177.          if (mask[i]) {
  178.             *aptr = alpha;
  179.          }
  180.          aptr++;
  181.       }
  182.    }
  183.    else {
  184.       for (i=0;i<n;i++) {
  185.          *aptr++ = alpha;
  186.       }
  187.    }
  188. }
  189.  
  190.  
  191. void gl_write_alpha_pixels( GLcontext* ctx,
  192.                             GLuint n, const GLint x[], const GLint y[],
  193.                             const GLubyte alpha[], const GLubyte mask[] )
  194. {
  195.    GLuint i;
  196.  
  197.    if (mask) {
  198.       for (i=0;i<n;i++) {
  199.          if (mask[i]) {
  200.             GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  201.             *aptr = alpha[i];
  202.          }
  203.       }
  204.    }
  205.    else {
  206.       for (i=0;i<n;i++) {
  207.          GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  208.          *aptr = alpha[i];
  209.       }
  210.    }
  211. }
  212.  
  213.  
  214. void gl_write_mono_alpha_pixels( GLcontext* ctx,
  215.                                  GLuint n, const GLint x[], const GLint y[],
  216.                                  GLubyte alpha, const GLubyte mask[] )
  217. {
  218.    GLuint i;
  219.  
  220.    if (mask) {
  221.       for (i=0;i<n;i++) {
  222.          if (mask[i]) {
  223.             GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  224.             *aptr = alpha;
  225.          }
  226.       }
  227.    }
  228.    else {
  229.       for (i=0;i<n;i++) {
  230.          GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  231.          *aptr = alpha;
  232.       }
  233.    }
  234. }
  235.  
  236.  
  237.  
  238. void gl_read_alpha_span( GLcontext* ctx,
  239.                          GLuint n, GLint x, GLint y, GLubyte alpha[] )
  240. {
  241.    GLubyte *aptr = ALPHA_ADDR( x, y );
  242.    GLuint i;
  243.    for (i=0;i<n;i++) {
  244.       alpha[i] = *aptr++;
  245.    }
  246. }
  247.  
  248.  
  249. void gl_read_alpha_pixels( GLcontext* ctx,
  250.                            GLuint n, const GLint x[], const GLint y[],
  251.                            GLubyte alpha[], const GLubyte mask[] )
  252. {
  253.    GLuint i;
  254.    for (i=0;i<n;i++) {
  255.       if (mask[i]) {
  256.          GLubyte *aptr = ALPHA_ADDR( x[i], y[i] );
  257.          alpha[i] = *aptr;
  258.       }
  259.    }
  260. }
  261.  
  262.  
  263.  
  264.